<?php
/**
 * Created by PhpStorm.
 * User: moonlight
 * Date: 2015/5/24
 * Time: 23:28
 */     

namespace Home\Controller;


use Think\Controller;

class UserController extends Controller{
    public function index(){
        $this->display();
    }
    
    //注册
    public function register(){
        if(IS_POST){
            $d = D('User')->register();
            if ($d['status'] == 1) {
                $this->success('注册成功！',U('User/login'),3);
            }else{
                $this->ajaxReturn($d);
            }
        }else{
            R('Index/isSession');
            $this->display();
        }
    }
    //登录
    public function login(){
        //session_start();
        if(IS_POST){
            $data = I('post.');
            $d = D('User')->login($data);
            if($d['status']==1){

                $this->success('登录成功！',U('Index/index'),3);

            }else{
                //dump('b');
                session('[destroy]'); 
                $this->ajaxReturn($d);
            }   
        }else{
            R('Index/isSession');
            $this->display();
        }
        //dump('bbbb');
    }
    /**
     * 退出登录
     */
    public function userExit(){
        session('[destroy]');
        header("Location:".U('Index/index'));
    }
    /**
     * 用户个人管理
     */
    public function userManage(){
        R('Index/isSession');
        if (session('?username')) {
            //判断用户信息是否完善
            //D("User")->isComplete();
            $this->getUserInfo();
            $this->display();
        } else {
            $this->error('抱歉！您还未登录', U('User/login'), 3);
        }
    }

    public function getUserInfo(){
        $User   =   D('User');
        $userInfo   =   $User->getUserData(session('user_id'));
        $this->assign('userInfo',$userInfo);
    }

    public function updateUserInfo(){
        $data = I('post.');
        $result = D('User')->updateUserInfo($data);
        dump($result);
        return $result;
    }
    /**
     * 将论坛列表的用户id转为name
     */
    public function idToName($data){
        //dump($data);
        if(count($data)){
            return  D('User')->idToName($data);
        }else{
            return false;
        }


    }
    
}

